﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Pages_ManagerPages_ManagerHome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Role"] != null && "Manager".Equals(Session["Role"]) == false)
        {
            Response.Redirect("~/Pages/HomePage/Login.aspx");
        }
        
        if (!IsPostBack)
        {
            getStaff();
        }
    }
    private void getAllStudents()
    {
        ManagerBUS m = new ManagerBUS();
        DataSet ds = m.getAllStudent();
        if (ds.Tables[0].Rows.Count==0)
        {
            Response.Write("<script>alert('No Student')</script>");
        }
        else
        {
            grvStudent.DataSource = ds.Tables[0];
            grvStudent.DataBind();
        }
    }
    private void getAllStaffs()
    {
        ManagerBUS m = new ManagerBUS();
        DataSet ds = m.getAllStaff();
        if (ds.Tables[0].Rows.Count == 0)
        {
            Response.Write("<script>alert('No Staff')</script>");
        }
        else
        {
            grvStaff.DataSource = ds.Tables[0];
            grvStaff.DataBind();
        }
    }
    private void getStaff()
    {
        ManagerBUS m = new ManagerBUS();
        DataSet ds = m.getAllStaff();
        if (ds.Tables[0].Rows.Count == 0)
        {
            Response.Write("<script>alert('No Staff')</script>");
        }
        else
        {
            grvStaffList.DataSource = ds.Tables[0];
            grvStaffList.DataBind();
        }
    }
    private void getAllCompetitions()
    {
        ManagerBUS manager = new ManagerBUS();
        DataSet ds = manager.getAllCompetition();
        if (ds== null)
        {
            Response.Write("<script>alert('No Competition')</script>");
        }
        else
        {
            grvCompetition.DataSource = ds.Tables[0];
            grvCompetition.DataBind();
        }
        
    }
    protected void grvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='updateStudent.aspx?id=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void grvCompetition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='updateCompetition.aspx?id=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void grvStaff_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='updateStaff.aspx?id=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void btnSeachStudent_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "studentList", "displayBlock('studentList');", true);
        ManagerBUS m = new ManagerBUS();
        string name = txtSeachStudent.Text;
        if ("".Equals(name))
        {
            getAllStudents();
        }
        else {
            grvStudent.DataSource = m.searchStudentByName(name).Tables[0];
            grvStudent.DataBind();
        }

    }
    protected void btnSeachCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlock('competitionList');", true);
        ManagerBUS m = new ManagerBUS();
        string name = txtSeachCompetition.Text;
        if ("".Equals(name))
        {
            getAllCompetitions();
        }
        else
        {
            grvCompetition.DataSource = m.searchCompetition(name).Tables[0];
            grvCompetition.DataBind();
        }
    }
    protected void btnSeachStaff_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "staffList", "displayBlock('staffList');", true);
    }
    protected void btnDisplayAllStudent_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "studentList", "displayBlock('studentList');", true);
        getAllStudents();
    }
    protected void btnDisplayAllStaffs_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "staffList", "displayBlock('staffList');", true);
        getAllStaffs();
    }
    protected void btnDisplayAllCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "competitionList", "displayBlock('competitionList');", true);
        getAllCompetitions();
    }
    protected void btnAddStudent_Click(object sender, EventArgs e)
    {
        string pass = txtPass.Text;
        string confirmPass = txtConfirmSt.Text;
        if (!pass.Equals(confirmPass))
        {
            lblWarningAddStudent.Text = "Confirm Pass not correct";
            return;
        }
        Student s = new Student();
        s.Id = -1;
        s.Username = txtUserName.Text;
        s.Password = txtPass.Text;
        s.Fullname = txtFullName.Text;
        s.Email = txtEmail.Text;
        s.PhoneNumber = txtPhone.Text;
        s.Major = txtMajor.Text;
        s._class1 = txtClass.Text;
        s.Address = txtAddress.Text;
        ManagerBUS manager = new ManagerBUS();
        if (manager.insertStudent(s))
        {
            Response.Redirect("~/Pages/HomePage/Success.aspx");
        }
        else
        {
            lblWarningAddStudent.Text = "Failed";
        }
        
    }
    protected void btnAddStaff_Click(object sender, EventArgs e)
    {
        string pass = txtPassStaff.Text;
        string confirmPass = txtConfirmStaff.Text;
        if (!pass.Equals(confirmPass))
        {
            lblWarningAddStaff.Text = "Confirm Pass not correct";
            return;
        }
            Staff s = new Staff();
            s.Id = -1;
            s.Username = txtUserNameStaff.Text;
            s.Password = txtPassStaff.Text;
            s.Fullname = txtFullNameStaff.Text;
            s.Email = txtEmailStaff.Text;
            s.PhoneNumber = txtPhoneStaff.Text;
            s.Major = txtMajorStaff.Text;
            s.Address = txtAddressStaff.Text;
            ManagerBUS manager = new ManagerBUS();
            if (manager.insertStaff(s))
            {
                Response.Redirect("~/Pages/HomePage/Success.aspx");
            }
            else
            {
                lblWarningAddStaff.Text = "Failed";
            }
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "changePass", "displayBlock('changePass');", true);
        string userName = Session["userName"].ToString();
        string currentPass = txtCurrentPass.Text;
        ManagerBUS manager = new ManagerBUS();
        if (!((manager.managerLogin(userName, currentPass).Tables[0].Rows.Count) > 0))
        {
            lblWarningChangePass.Text = "Username and CurrentPass not correct";
            return;
        }
        string newPass = txtNewPass.Text;
        string confirmPass = txtConfirmPass.Text;
        if (!(newPass.Equals(confirmPass)))
        {
            lblWarningChangePass.Text = "ConfirmPass not correct";
            return;
        }
        Manager m = manager.searchManagerById(Convert.ToInt32(Session["Id"].ToString()));
        m.Password = newPass;
        if (manager.updateManager(m))
        {
            Response.Redirect("~/Pages/HomePage/Success.aspx");
        }
        else
        {
            lblWarningChangePass.Text = "Failed";
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "updateInfo", "displayBlock('updateInfo');", true);
        ManagerBUS manager = new ManagerBUS();
        Manager m = manager.searchManagerById(Convert.ToInt32(Session["Id"].ToString()));
        m.Fullname=txtFullNameUpdate.Text;
        m.Address=txtAddressUpdate.Text;
        m.Email=txtEmailUpdate.Text;
        m.PhoneNumber=txtPhoneUpdate.Text;
        if (manager.updateManager(m))
        {
            Response.Redirect("~/Pages/HomePage/Success.aspx");
        }
        else
        {
            lblWarningUpdateInfo.Text = "Failed";
        }
    }
    protected void addComp_Click(object sender, EventArgs e)
    {
            Competition comp = new Competition();
            ManagerBUS m = new ManagerBUS();
            comp.Name = txtCompetitionName.Text;
            comp.PostDate = DateTime.Now.Date.ToString();
            comp.StartDate = txtCompetitionStartDate.Text;
            comp.EndDate = txtCompetitionEndDate.Text;
            comp.Description = txtCompetitionDescription.Text;
            comp.Quotation = txtCompetitionQuotation.Text;
            comp.FirstPrize = txtFirstPrize.Text;
            comp.SecondPrize = txtSecondPrize.Text;
            comp.ThirdPrize = txtThirdPrize.Text;
            comp.Manager.Id = Convert.ToInt16(Session["Id"]);
            string fileName = Path.GetFileName(fileCompImage.PostedFile.FileName);
            string extension = Path.GetExtension(fileName);
            //fileCompImage.PostedFile.SaveAs(Server.MapPath("~/Images/Competition/") + fileName);
            comp.CompImage = extension;
            DataSet ds = m.insertCompetition(comp);
            if (ds.Tables[0].Rows.Count > 0)
            {
                int compId=Convert.ToInt16( ds.Tables[0].Rows[0]["compImg"]);
                string saveFile = compId + extension;
                fileCompImage.PostedFile.SaveAs(Server.MapPath("~/Images/Competition/") + saveFile);
                foreach (GridViewRow item in grvStaffList.Rows)
                {
                    CheckBox temp = (CheckBox)item.Cells[0].FindControl("ckChose");
                    if (temp.Checked)
                    {
                        int staffId = Convert.ToInt16(grvStaffList.Rows[item.RowIndex].Cells[0].Text);
                        m.practiceStaffComp(compId, staffId);
                    }
                }
                Response.Redirect("~/Pages/HomePage/Success.aspx");
            }
    }
    protected void btnDisplayAll_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "designOfCompetition", "displayBlock('designOfCompetition');", true);
        ManagerBUS manager = new ManagerBUS();
        DataSet ds = manager.getAllCompetition();
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
        }
        else
        {
            grvComp.DataSource = ds.Tables[0];
            grvComp.DataBind();
        }
    }
    protected void btnSeachComp_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "designOfCompetition", "displayBlock('designOfCompetition');", true);
        ManagerBUS m = new ManagerBUS();
        string name = txtSeachCompetition.Text;
        if ("".Equals(name))
        {
            DataSet ds = m.getAllCompetition();
            if (ds == null)
            {
                Response.Write("<script>alert('No Competition')</script>");
            }
            else
            {
                grvComp.DataSource = ds.Tables[0];
                grvComp.DataBind();
            }
        }
        else
        {
            grvComp.DataSource = m.searchCompetition(name).Tables[0];
            grvComp.DataBind();
        }
    }
    protected void grvComp_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
        e.Row.Attributes.Add("style", "cursor:pointer;");
        e.Row.Attributes.Add("onclick", "location='DesignOfCompetition.aspx?id=" + e.Row.Cells[0].Text + "'");
    }
}